<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\shiyao;

use app\api\model\User as UserModel;
use app\api\service\User as UserService;
use app\common\model\shiyao\Project as ProjectModel;
use cores\exception\BaseException;
use think\facade\Db;

/**
 * 模型
 * Class
 * @package app\common\model
 */
class Project extends ProjectModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	/**
	 * 列表
	 * @return \think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws BaseException
	 */
	public function getList() {

		$filter = [];
		$user = UserService::getCurrentLoginUser();
		$userId = $user['user_id'];
		$role = json_decode($user['role'], true);
		//协助pm

		//$pm = Db::name('shiyao_project_pm')->where('pm_id', $userId)->field('project_id')->select()->toArray();
		//$projectIds = array_column($pm, 'project_id');

		if ($role && in_array(10006, $role)) {
			//申办方管理员
			$filter[] = ['p.company_id', '=', $user['company_id']];
		} else if ($role && in_array(10009, $role)) {
			//申办方项目管理
			$filter[] = ['p.company_id', '=', $user['company_id']];
		} else {

			$filter[] = ['p.status', '=', 1]; //审核通过

			//审批人员
			$projectIds = Db::name('shiyao_project_center_pm')->where('pm_id', $userId)
				->group('project_id')->select()->toArray();

			$projectIds = array_column($projectIds, 'project_id');
			//自己添加的项目
			$pm = Db::name('shiyao_project_center')->where('pm_id', $userId)->field('project_id')->group('project_id')->select()->toArray();
			$projectIds2 = array_column($pm, 'project_id');
			$newprojectIds = array_merge($projectIds, $projectIds2);

			if ($newprojectIds) {
				$filter[] = ['p.project_id', 'in', $newprojectIds];
			} else {
				$newprojectIds = [];
				//为了返回空
				$filter[] = ['p.store_id', '=', 1];
			}

		}
		$list = $this->alias('p')
			->where('is_delete', '=', 0)
			->where($filter)
			->order('project_id desc')
			->paginate(15);

		foreach ($list as &$item) {
			$count = Db::name('shiyao_project_pay_log')->alias('pay')
				->join('shiyao_project p', 'p.project_id=pay.project_id')
				->join('shiyao_project_center center', 'center.id=pay.center_id')
				->where('pay.pm_status', 0)
				->where('pay.pi_status', 1)
				->where($filter)
				->where('pay.project_id', $item['project_id'])
				->where('p.level', 2)
				->count();
			$item['center_count'] = Db::name('shiyao_project_center')->where('project_id', $item['project_id'])

				->count();
			$item['wait'] = $count > 0 ? 1 : 0;
		}

		return $list;

	}
	public function getListAll() {

		$filter = [];
		$user = UserService::getCurrentLoginUser();
		$companyId = $user['company_id'];

		return $this
			->where('is_delete', '=', 0)
			->where('company_id', '=', $companyId)

			->order('project_id desc')
			->select()->toArray();

	}

	/**
	 * 新增
	 * @param array $data
	 * @return mixed
	 * @throws BaseException
	 */
	public function add(array $data) {
		// 当前用户信息
		$user = UserService::getCurrentLoginUser(true);

		// 添加
		return $this->transaction(function () use ($user, $data) {

			$data = array_merge($data, [
				'pm_id' => $user['user_id'],
				'company_id' => $user['company_id'],
				'store_id' => self::$storeId,
			]);
			$this->save($data);

			return true;
		});
	}

	/**
	 * 编辑收货地址
	 * @param array $data
	 * @return bool
	 * @throws BaseException
	 */
	public function edit(array $data) {

		// 更新收货地址
		return $this->save($data) !== false;
	}

	/**
	 * 删除
	 * @return bool
	 * @throws BaseException
	 */
	public function remove() {
		// 查询当前是否为默认地址
		$user = UserService::getCurrentLoginUser(true);
		// 清空默认地址
		if ($user['address_id'] == $this['address_id']) {
			UserModel::updateBase(['address_id' => 0], ['user_id' => $this['user_id']]);
		}
		// 标记为已删除
		return $this->save(['is_delete' => 1]);
	}

	/**
	 * 收货地址详情
	 * @param int $addressId
	 * @return UserAddress|array|null
	 * @throws BaseException
	 */
	public static function detail(int $projectId) {
		$userId = UserService::getCurrentLoginUserId();
		$detail = self::get(['project_id' => $projectId]);
		if (empty($detail)) {
			throwError('未找到该项目');
		}
		return $detail;
	}
}
